import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/foundation/change_notifier.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/sub/TransferMessagePage.dart';
import 'package:im_app/im/util/DateUtil.dart';

import '../../pojo/ImPojo.dart';
import '../../util/UiUtil.dart';
import '../bloc/MessageListCubit.dart';
import '../bloc/MessageSendCubit.dart';
import '../bloc/TransferMessageCubit.dart';
import '../view/component/UiUtil.dart';
import '../view/component/pane/ImMessageHandler.dart';

class AudioSelectionControls extends MaterialTextSelectionControls {
  static const double _kToolbarContentDistanceBelow = 20.0;
  static const double _kToolbarContentDistance = 8.0;

  final ImMessage message;
  final MessageSendCubit messageSendCubit;
  final MessageListCubit messageListCubit;

  AudioSelectionControls({required this.message, required this.messageSendCubit, required this.messageListCubit});

  @override
  Widget buildToolbar(
      BuildContext context,
      Rect globalEditableRegion,
      double textLineHeight,
      Offset selectionMidpoint,
      List<TextSelectionPoint> endpoints,
      TextSelectionDelegate delegate,
      ValueListenable<ClipboardStatus>? clipboardStatus,
      Offset? lastSecondaryTapDownPosition) {
    final TextSelectionPoint startTextSelectionPoint = endpoints[0];
    final TextSelectionPoint endTextSelectionPoint = endpoints.length > 1 ? endpoints[1] : endpoints[0];
    final Offset anchorAbove = Offset(
      globalEditableRegion.left + selectionMidpoint.dx,
      globalEditableRegion.top + startTextSelectionPoint.point.dy - textLineHeight - _kToolbarContentDistance,
    );
    final Offset anchorBelow = Offset(
      globalEditableRegion.left + selectionMidpoint.dx,
      globalEditableRegion.top + endTextSelectionPoint.point.dy + _kToolbarContentDistanceBelow,
    );

    return TextSelectionToolbar(
      anchorAbove: anchorAbove,
      anchorBelow: anchorBelow,
      toolbarBuilder: (context, _) => Material(
        child: Container(
          width: 300,
          height: 90, // 150
          decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10)), color: Colors.black54),
          child: _buildPopupPane(context, delegate),
        ),
      ),
      children: const [SizedBox.shrink()],
    );
  }

  Widget _buildPopupPane(BuildContext context, TextSelectionDelegate delegate) {
    return GridView.count(
      crossAxisCount: 4,
      childAspectRatio: 1,
      padding: const EdgeInsets.all(10),
      children: <Widget>[
        TextButton( // 播放
            onPressed: () {
              var messageHandler = ImMessageHandler();
              messageHandler.playAudio(context, message);
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [_buildSvg('assets/svg/play.svg'), const Text('播放', style: TextStyle(color: Colors.white)
                ,)],
            )
        ),
        // TextButton( // transferToText
        //     onPressed: () {
        //       var messageHandler = ImMessageHandler();
        //       messageHandler.transferAudioToText(context, message);
        //     },
        //     child: Column(
        //       mainAxisAlignment: MainAxisAlignment.center,
        //       crossAxisAlignment: CrossAxisAlignment.center,
        //       children: [_buildSvg('assets/svg/audio_to_text.svg'),
        //         const Text('转文字', style: TextStyle(color: Colors.white),)],
        //     )
        // ),

        // TextButton( // 收藏
        //     onPressed:  () async {
        //       var messageHandler = ImMessageHandler();
        //       await messageHandler.collectMessages([message]);
        //       UiUtil.showSnackBar(context, '消息收藏成功');
        //       delegate.hideToolbar();
        //     },
        //     child: Column(
        //       mainAxisAlignment: MainAxisAlignment.center,
        //       crossAxisAlignment: CrossAxisAlignment.center,
        //       children: [_buildSvg('assets/svg/collect.svg'), const Text('收藏', style: TextStyle(color: Colors.white),)],
        //     )
        // ),
        TextButton( // 删除
            onPressed: () {
              var messageHandler = ImMessageHandler();
              messageHandler.deleteMessages([message]);
              UiUtil.showSnackBar(context, '消息删除成功');
              delegate.hideToolbar();
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [_buildSvg('assets/svg/delete.svg'), const Text('删除', style: TextStyle(color: Colors.white),)],
            )
        ),
        if (!DateUtil.hasExceededTime(message.serverReceiveTime??message.createTime, 2))
          TextButton( // 撤回
              onPressed: () async {
                if (!DateUtil.hasExceededTime(message.serverReceiveTime??message.createTime, 2)) {
                  var messageHandler = ImMessageHandler();
                  await messageHandler.withdrawMessage(message);
                  UiUtil.showSnackBar(context, '消息撤回成功');
                }
                delegate.hideToolbar();
              },
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [_buildSvg('assets/svg/withdraw.svg'), const Text('撤回', style: TextStyle(color: Colors.white)
                  ,)],
              )
          ),

        TextButton( // 多选
            onPressed: () {
              messageListCubit.add(MessageListSelectableEvent(true));
              delegate.hideToolbar();
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [_buildSvg('assets/svg/select_all.svg'), const Text('多选', style: TextStyle(color: Colors
                  .white),)],
            )
        ),
        TextButton( // 引用
            onPressed: () {
              messageSendCubit.add(MessageSendRefMessageEvent(message));
              delegate.hideToolbar();
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [_buildSvg('assets/svg/ref.svg'), const Text('引用', style: TextStyle(color: Colors.white),)],
            )
        ),
      ],
    );
  }

  Widget _buildSvg(String assetFile) {
    return buildSvgView(assetFile, 20, 20, Colors.white);
  }
}
